package com.ruoyi.rms.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.rms.domain.DishesMessage;
import com.ruoyi.rms.enums.DishesStatus;
import com.ruoyi.rms.service.IDishesMessageService;
import com.ruoyi.rms.utils.IdUtils;
import com.ruoyi.rms.utils.RetUtils;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.rms.mapper.CategoryMessageMapper;
import com.ruoyi.rms.domain.CategoryMessage;
import com.ruoyi.rms.service.ICategoryMessageService;

/**
 * 菜品类别管理Service业务层处理
 * 
 * @author wyl
 * @date 2024-03-07
 */
@Service
@Slf4j
public class CategoryMessageServiceImpl implements ICategoryMessageService 
{
    @Autowired
    private CategoryMessageMapper categoryMessageMapper;

    @Autowired
    private IDishesMessageService dishesMessageService;

    /**
     * 查询菜品类别管理
     * 
     * @param categoryId 菜品类别管理主键
     * @return 菜品类别管理
     */
    @Override
    public CategoryMessage selectCategoryMessageByCategoryId(Long categoryId)
    {
        return categoryMessageMapper.selectCategoryMessageByCategoryId(categoryId);
    }

    /**
     * 查询菜品类别管理列表
     * 
     * @param categoryMessage 菜品类别管理
     * @return 菜品类别管理
     */
    @Override
    public List<CategoryMessage> selectCategoryMessageList(CategoryMessage categoryMessage)
    {
        List<CategoryMessage> categoryMessageList = categoryMessageMapper.selectCategoryMessageList(categoryMessage);
        if (categoryMessageList.isEmpty()) return categoryMessageList;

        // 联查每个类别下拥有的菜品数量（没有下架的）
        for (CategoryMessage message : categoryMessageList) {
            Long categoryId = message.getCategoryId();
            List<DishesMessage> dishesMessageList = dishesMessageService.selectDishesMessageListByCategoryAndStatus(categoryId, DishesStatus.TAKE_OFF.getStatus());
            if (dishesMessageList.isEmpty()) message.setDishesCount(0);
            else message.setDishesCount(dishesMessageList.size());
        }

        return categoryMessageList;
    }

    /**
     * 新增菜品类别管理
     * 
     * @param categoryMessage 菜品类别管理
     * @return 结果
     */
    @Override
    public int insertCategoryMessage(CategoryMessage categoryMessage)
    {
        String categoryName = categoryMessage.getCategoryName();
        if (categoryName == null) {
            log.error("类别名称不能为空");
            return RetUtils.ERROR;
        }

        // 判断名称是否与已有名称重复
        List<CategoryMessage> categoryMessageList = categoryMessageMapper.selectCategoryMessageByCategoryName(categoryName);
        if (!categoryMessageList.isEmpty()) {
            log.error("类别名称与已有类别名称重复，添加失败");
            return RetUtils.ERROR;
        }

        categoryMessage.setCategoryId(IdUtils.createId());
        categoryMessage.setCreateTime(DateUtils.getNowDate());
        categoryMessage.setCreateBy(SecurityUtils.getUsername());
        return categoryMessageMapper.insertCategoryMessage(categoryMessage);
    }

    /**
     * 修改菜品类别管理
     * 
     * @param categoryMessage 菜品类别管理
     * @return 结果
     */
    @Override
    public int updateCategoryMessage(CategoryMessage categoryMessage)
    {
        String categoryName = categoryMessage.getCategoryName();
        if (categoryName == null) {
            log.error("类别名称不能为空");
            return RetUtils.ERROR;
        }

        // 判断修改后的类别名称是否与其他名称重复
        Long categoryId = categoryMessage.getCategoryId();
        List<CategoryMessage> categoryMessageList = categoryMessageMapper.selectCategoryMessageByCategoryIdAndName(categoryId, categoryName);
        if (!categoryMessageList.isEmpty()) {
            log.error("修改后的名称与其他类别名称重复，修改失败");
            return RetUtils.ERROR;
        }

        categoryMessage.setUpdateTime(DateUtils.getNowDate());
        categoryMessage.setUpdateBy(SecurityUtils.getUsername());
        return categoryMessageMapper.updateCategoryMessage(categoryMessage);
    }

    /**
     * 批量删除菜品类别管理
     * 
     * @param categoryIds 需要删除的菜品类别管理主键
     * @return 结果
     */
    @Override
    public int deleteCategoryMessageByCategoryIds(Long[] categoryIds)
    {
        return categoryMessageMapper.deleteCategoryMessageByCategoryIds(categoryIds);
    }

    /**
     * 删除菜品类别管理信息
     * 
     * @param categoryId 菜品类别管理主键
     * @return 结果
     */
    @Override
    public int deleteCategoryMessageByCategoryId(Long categoryId)
    {
        return categoryMessageMapper.deleteCategoryMessageByCategoryId(categoryId);
    }
}
